﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DataObjects.EntityFramework.ModelMapper;
using DataObjects.StandartInterfaces.Dictionary;

namespace DataObjects.EntityFramework.Implementation.Dictionary
{
    /// <summary>
    /// CustomerDao  class
    /// </summary>
    public class EntityAutoBusinessDao : IAutoBusinessDao
    {

        public List<BusinessObjects.BussinessDictionary.AutoBusiness> GetPaged(int unitId, int currentPage)
        {
            using (var context = DataObjectFactory.CreateContext())
            {
                var pageSize = 0;
                var result = context.AutoBusinesses.AsQueryable().Where(x => x.UnitID == unitId).OrderByDescending(x => x.AutoBusinessID).ToList();
                var option = context.DBOptions.SingleOrDefault(x => x.Code == "NumberOfPage");
                if (option != null)
                {
                    int.TryParse(option.Value, out pageSize);
                }
                if (pageSize > 0 && currentPage > 0)
                {
                    var start = (currentPage - 1) * pageSize;
                    result = result.Skip(start).Take(pageSize).ToList();
                }
                return result.Map().ToList();
            }
        }

        public List<BusinessObjects.BussinessDictionary.AutoBusiness> GetAutoBusinessByUnit(int unitId)
        {
            using (var context = DataObjectFactory.CreateContext())
            {
                var result = context.AutoBusinesses.AsQueryable().Where(x => x.UnitID == unitId).OrderByDescending(x => x.AutoBusinessID).ToList();
                return result.Map().ToList();
            }
        }

        public int GetPageSize(int unitId)
        {
            using (var context = DataObjectFactory.CreateContext())
            {
                var pageSize = 0;
                var option = context.DBOptions.SingleOrDefault(x => x.Code == "NumberOfPage");
                var totalRow = context.AutoBusinesses.Count();
                if (option != null)
                {
                    int.TryParse(option.Value, out pageSize);
                }
                if (pageSize > 0)
                {
                    var round = totalRow % pageSize;
                    if (round > 0)
                    {
                        return totalRow / pageSize + 1;
                    }
                    return totalRow / pageSize;

                }
                return 1;
            }
        }

        public BusinessObjects.BussinessDictionary.AutoBusiness GetAutoBusiness(int autoBusinessId)
        {
            using (var context = DataObjectFactory.CreateContext())
            {
                return context.AutoBusinesses.SingleOrDefault(x => x.AutoBusinessID == autoBusinessId).Map();
            }
        }

        public string InsertAutoBusiness(BusinessObjects.BussinessDictionary.AutoBusiness autoBusiness)
        {
            try
            {
                using (var context = DataObjectFactory.CreateContext())
                {
                    context.AutoBusinesses.Add(autoBusiness.Map());
                    context.SaveChanges();
                }
            }
            catch (Exception e)
            {
                return e.Message;
            }
            return null;
        }

        public string UpdateAutoBusiness(BusinessObjects.BussinessDictionary.AutoBusiness autoBusiness)
        {
            try
            {
                using (var context = DataObjectFactory.CreateContext())
                {
                    var result = context.AutoBusinesses.SingleOrDefault(x => x.AutoBusinessID == autoBusiness.AutoBusinessId);
                    if (result != null)
                    {
                        result.AutoBusinessID = autoBusiness.AutoBusinessId;
                        result.UnitID = autoBusiness.UnitId;
                        result.AutoBusinessCode = autoBusiness.AutoBusinessCode;
                        result.AutoBusinessName = autoBusiness.AutoBusinessName;
                        result.Description = autoBusiness.Description;
                        result.IsActive = autoBusiness.IsActive;

                        result.CreditAccountNumber = autoBusiness.CreditAccountNumber;
                        result.DebitAccountNumber = autoBusiness.DebitAccountNumber;
                        result.Description = autoBusiness.Description;
                        result.RefTypeID = autoBusiness.RefTypeId;

                        context.SaveChanges();
                    }
                }
            }
            catch (Exception e)
            {
                return e.Message;
            }
            return null;
        }

        public string DeleteAutoBusiness(BusinessObjects.BussinessDictionary.AutoBusiness autoBusiness)
        {
            try
            {
                using (var context = DataObjectFactory.CreateContext())
                {
                    var result = context.AutoBusinesses.SingleOrDefault(x => x.AutoBusinessID == autoBusiness.AutoBusinessId);
                    if (result != null)
                    {
                        context.AutoBusinesses.Remove(result);
                        context.SaveChanges();
                    }
                }
            }
            catch (Exception e)
            {
                return e.Message;
            }
            return null;
        }
    }
}
